package sisicotren.ui.windows;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dialog;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.text.NumberFormat;

import javax.swing.JButton;

public class ConfigurationDialog {

	private JDialog jDialog = null;  //  @jve:decl-index=0:visual-constraint="121,23"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JPanel jPanel2 = null;
	private JPanel jPanel3 = null;
	private JTextField arribosResultTextField = null;
	private JTextField servicioResultTextField = null;
	private JLabel arribosLabel = null;
	private JLabel servicioLabel = null;
	private JLabel anchoMolineteLabel = null;
	private JTextField anchoCanalTextField = null;
	private JLabel supTotalDipLabel = null;
	private JTextField supTotalDispTextField = null;
	private JTextField espacioInterMoliTextField = null;
	private JButton aceptarConfigButton = null;
	private JButton cancelarButton = null;
	private MainWindow dialogOwner = null;
	private Integer totalChannels = 0;  //  @jve:decl-index=0:
	private ConfigurationDialog thiz = this;
	private JLabel espacioInterMolineteLabel;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JTextField maxToleranceInQueueTextField = null;
	private JLabel jLabel1 = null;
	private JTextField maxChannelsForOptimizationTextField = null;
	private JLabel jLabel2 = null;
	private JTextField poblationSizeTextField = null;
	
	public ConfigurationDialog(MainWindow dialogOwner) {
		this.dialogOwner = dialogOwner;
		this.getJDialog();
	}
	
	public void open() {
		this.getJDialog().setVisible(true);
	}
	
	public void close() {
		this.getJDialog().dispose();
	}
	
	/**
	 * This method initializes jDialog	
	 * 	
	 * @return javax.swing.JDialog	
	 */
	protected JDialog getJDialog() {
		if (jDialog == null) {
			if(this.dialogOwner == null) {
				jDialog = new JDialog();
			}
			else {
				jDialog = new JDialog(this.dialogOwner.getJFrame());
			}
			jDialog.setTitle("Configuracion");
			jDialog.setContentPane(getJContentPane());
			jDialog.setLocationByPlatform(true);
			jDialog.setModalityType(Dialog.DEFAULT_MODALITY_TYPE);
			jDialog.pack();
		}
		return jDialog;
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.WEST);
			jContentPane.add(getJPanel2(), BorderLayout.NORTH);
			jContentPane.add(getJPanel3(), BorderLayout.SOUTH);
			jContentPane.add(getJPanel1(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setColumns(2);
			gridLayout.setRows(3);
			servicioLabel = new JLabel();
			servicioLabel.setText("Servicio (clientes/min)");
			servicioLabel.setName("jLabel1");
			arribosLabel = new JLabel();
			arribosLabel.setText("Arribos (clientes/min)");
			arribosLabel.setName("jLabel");
			jPanel = new JPanel();
			jPanel.setLayout(gridLayout);
			jPanel.add(arribosLabel, null);
			jPanel.add(getJTextField(), null);
			jPanel.add(servicioLabel, null);
			jPanel.add(getJTextField1(), null);
			jPanel.setBorder(BorderFactory.createTitledBorder("Sistema"));
		}
		return jPanel;
	}

	private JPanel getJPanel2() {
		if(jPanel2 == null) {
			supTotalDipLabel = new JLabel();
			supTotalDipLabel.setText("Superficie Total Disponible (en metros)");
			anchoMolineteLabel = new JLabel();
			anchoMolineteLabel.setText("Ancho de un molinete (en metros)");
			espacioInterMolineteLabel = new JLabel();
			espacioInterMolineteLabel.setText("Espacio entre molinetes (en metros)");
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(3);
			gridLayout1.setColumns(2);
			jPanel2 = new JPanel();
			jPanel2.setLayout(gridLayout1);
			jPanel2.add(anchoMolineteLabel, null);
			jPanel2.add(getJTextField2(), null);
			jPanel2.add(espacioInterMolineteLabel, null);
			jPanel2.add(getJTextField4(), null);
			jPanel2.add(supTotalDipLabel, null);
			jPanel2.add(getJTextField3(), null);
			jPanel2.setBorder(BorderFactory.createTitledBorder("Molinetes"));
		}
		return jPanel2;
	}
	
	private JTextField getJTextField4() {
		if (espacioInterMoliTextField == null) {
			espacioInterMoliTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
			espacioInterMoliTextField.setText(""+this.dialogOwner.getInterChannelSpace());
		}
		return espacioInterMoliTextField;
	}


	private JPanel getJPanel3() {
		if(jPanel3 == null) {
			jPanel3 = new JPanel();
			jPanel3.add(getJButton(), null);
			jPanel3.add(getJButton1(), null);
		}
		return jPanel3;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (arribosResultTextField == null) {
			arribosResultTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
			arribosResultTextField.setText(""+this.dialogOwner.getLambda());
			arribosResultTextField.setName("jTextField");
		}
		return arribosResultTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (servicioResultTextField == null) {
			servicioResultTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
			servicioResultTextField.setText(""+this.dialogOwner.getMu());
			servicioResultTextField.setName("jTextField1");
		}
		return servicioResultTextField;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (anchoCanalTextField == null) {
			anchoCanalTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
			anchoCanalTextField.setText(""+this.dialogOwner.getChannelWidth());
		}
		return anchoCanalTextField;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField3() {
		if (supTotalDispTextField == null) {
			supTotalDispTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
			supTotalDispTextField.setText(""+this.dialogOwner.getAvailableChannelsWidth());
		}
		return supTotalDispTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (aceptarConfigButton == null) {
			aceptarConfigButton = new JButton();
			aceptarConfigButton.setText("Aceptar");
			aceptarConfigButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(dialogOwner != null) {
						dialogOwner.setLambda(Double.parseDouble(arribosResultTextField.getText()));
						dialogOwner.setMu(Double.parseDouble(servicioResultTextField.getText()));
						dialogOwner.setTotalChannels(thiz.getTotalChannels());
						dialogOwner.setTotalQueues(thiz.getTotalChannels());
						dialogOwner.setAvailableChannelsWidth(
								Double.parseDouble(supTotalDispTextField.getText()));
						dialogOwner.setChannelWidth(Double.parseDouble(anchoCanalTextField.getText()));
						dialogOwner.setInterChannelSpace((Double.parseDouble(espacioInterMoliTextField.getText())));
						dialogOwner.setMaxChannelsForOptimization(Integer.parseInt(getMaxChannelsForOptimization().getText()));
						dialogOwner.setMaxToleranceInQueue(Integer.parseInt(getMaxToleranceInQueueTextField().getText()));
						dialogOwner.setPoblationSize(Integer.parseInt(getPoblationSizeTextField().getText()));
					}
					close();
				}
			});
		}
		return aceptarConfigButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (cancelarButton == null) {
			cancelarButton = new JButton();
			cancelarButton.setText("Cancelar");
			cancelarButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					close();
				}
			});
		}
		return cancelarButton;
	}

	public void setTotalChannels(Integer channels) {
		this.totalChannels = channels;
	}

	public Integer getTotalChannels() {
		return this.totalChannels;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Tamaño de la poblacion");
			jLabel1 = new JLabel();
			jLabel1.setText("Maxima cantidad de Canales");
			jLabel = new JLabel();
			jLabel.setText("Maxima tolerancia de cola");
			jPanel1 = new JPanel();
			GridLayout layout = new GridLayout();
			layout.setColumns(2);
			layout.setRows(3);
			jPanel1.setLayout(layout);
			jPanel1.setBorder(BorderFactory.createTitledBorder("Optimizacion"));
			jPanel1.add(jLabel, null);
			jPanel1.add(getMaxToleranceInQueueTextField(), null);
			jPanel1.add(jLabel1, null);
			jPanel1.add(getMaxChannelsForOptimization(), null);
			jPanel1.add(jLabel2, null);
			jPanel1.add(getPoblationSizeTextField(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getMaxToleranceInQueueTextField() {
		if (maxToleranceInQueueTextField == null) {
			maxToleranceInQueueTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
			maxToleranceInQueueTextField.setText(""+this.dialogOwner.getMaxToleranceInQueue());
		}
		return maxToleranceInQueueTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getMaxChannelsForOptimization() {
		if (maxChannelsForOptimizationTextField == null) {
			maxChannelsForOptimizationTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
			maxChannelsForOptimizationTextField.setText(""+this.dialogOwner.getMaxChannelsForOptimization());
		}
		return maxChannelsForOptimizationTextField;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPoblationSizeTextField() {
		if (poblationSizeTextField == null) {
			poblationSizeTextField = new JFormattedTextField(NumberFormat.getNumberInstance());
			poblationSizeTextField.setText(""+this.dialogOwner.getPoblationSize());
		}
		return poblationSizeTextField;
	}
	
}
